﻿#include "algo_dzft_form.h"
#include "ui_algo_dzft_form.h"
#include <QJsonObject>
#include "singleton.h"
Algo_DZFT_Form::Algo_DZFT_Form(QString chnid, QWidget* parent) :
    QWidget(parent),
    ui(new Ui::Algo_DZFT_Form), m_chnid(chnid)
{
    ui->setupUi(this);
}

Algo_DZFT_Form::~Algo_DZFT_Form()
{
    delete ui;
}

void Algo_DZFT_Form::on_btn_save_picture_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("SaveScenePicture", body);
}

void  Algo_DZFT_Form::onGetAlgoParam(QJsonObject obj)
{
    ui->sb_enable->setChecked(obj.value("enable").toInt());
    ui->led_thresh->setText(QString::number(obj.value("thresh").toDouble(), 'f', 10));
    ui->led_url->setText(obj.value("url").toString());
    QString eventType = obj.value("eventType").toString();
    if(eventType == "XJ_000")   ui->cbx_eventtype->setCurrentIndex(0);
    else if(eventType == "PD_000")   ui->cbx_eventtype->setCurrentIndex(1);
    else if(eventType == "ML_000")   ui->cbx_eventtype->setCurrentIndex(2);
    else if(eventType == "RQ_000")   ui->cbx_eventtype->setCurrentIndex(3);
    else if(eventType == "XW_000")   ui->cbx_eventtype->setCurrentIndex(4);
    else if(eventType == "ZC_000")   ui->cbx_eventtype->setCurrentIndex(5);
    else if(eventType == "JL_000")   ui->cbx_eventtype->setCurrentIndex(6);
    else if(eventType == "CY_000")   ui->cbx_eventtype->setCurrentIndex(7);
    else if(eventType == "HJ_000")   ui->cbx_eventtype->setCurrentIndex(8);
    else if(eventType == "SB_000")   ui->cbx_eventtype->setCurrentIndex(9);
    if(obj.value("alarmType").toString() != "")
    {
        ui->cbx_alarmtype->setCurrentIndex(obj.value("alarmType").toString().split("_").at(1).toInt() - 1);
    }
    ui->cbx_alarmgrade->setCurrentIndex(obj.value("alarmGrade").toInt());
    ui->led_ipcip->setText(obj.value("ipcIp").toString());
    ui->led_ipcuser->setText(obj.value("ipcUser").toString());
    ui->led_ipcpsw->setText(obj.value("ipcPassword").toString());
}

void Algo_DZFT_Form::on_btn_getparam_1_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("GetAlgoParam", body);
}

void Algo_DZFT_Form::on_btn_setparam_1_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    body.insert("enable", int(ui->sb_enable->getChecked()));
    body.insert("thresh", ui->led_thresh->text().toFloat());
    body.insert("url", ui->led_url->text());
    QStringList m_type = this->type();
    body.insert("eventType",  m_type.at(0));
    body.insert("alarmType", m_type.at(1));
    body.insert("alarmGrade", ui->cbx_alarmgrade->currentIndex());
    body.insert("ipcIp", ui->led_ipcip->text());
    body.insert("ipcUser", ui->led_ipcuser->text());
    body.insert("ipcPassword", ui->led_ipcpsw->text());
    Singleton::Instance()->onSendParam("SetAlgoParam", body);
}

QStringList Algo_DZFT_Form::type()
{
    QStringList type;
    QString eventtype;
    QString alarmtype;
    switch (ui->cbx_eventtype->currentIndex())
    {
        case 0:
            eventtype = "XJ_000";
            alarmtype = QString("XJ_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 1:
            eventtype = "PD_000";
            alarmtype = QString("PD_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 2:
            eventtype = "ML_000";
            alarmtype = QString("ML_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 3:
            eventtype = "RQ_000";
            alarmtype = QString("RQ_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 4:
            eventtype = "XW_000";
            alarmtype = QString("XW_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 5:
            eventtype = "ZC_000";
            alarmtype = QString("ZC_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 6:
            eventtype = "JL_000";
            alarmtype = QString("JL_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 7:
            eventtype = "CY_000";
            alarmtype = QString("CY_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 8:
            eventtype = "HJ_000";
            alarmtype = QString("HJ_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 9:
            eventtype = "SB_000";
            alarmtype = QString("SB_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        default:
            break;
    }
    type.append(eventtype);
    type.append(alarmtype);
    return  type;
}
